# -*- coding: utf-8 -*-
# @Time    : 2019/10/3 17:56
# @Author  : LiHaowen
# @Email   : lihaowen2017@163.com
# @File    : consumers.py
import json
from channels.generic.websocket import AsyncWebsocketConsumer


class NotificationsConsumer(AsyncWebsocketConsumer):
    """处理通知应用中的WebSocket请求"""

    async def connect(self):
        """建立连接"""
        if self.scope["user"].is_anonymous:
            # 未登录的用户拒绝连接
            await self.close()
        else:
            # 固定组名,视图层控制显示内容
            await self.channel_layer.group_add("notifications", self.channel_name)
            await self.accept()

    async def receive(self, text_data=None, bytes_data=None):
        """将接收到的信息返回给前端，视图传递的消息"""
        await self.send(text_data=json.dumps(text_data))

    async def disconnect(self, code):
        """断开连接"""
        await self.channel_layer.group_discard('notifications', self.channel_name)



